package test05;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class RecieveSocket {
    public static void main(String[] args) throws IOException {

        // 先找快递公司
        DatagramSocket datagramSocket = new DatagramSocket(10036);

        byte[] bytes = new byte[1024];

        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);

        while (true) {

            datagramSocket.receive(datagramPacket);
            //解析数据
            byte[] data = datagramPacket.getData();
            int len = datagramPacket.getLength();
            InetAddress address = datagramPacket.getAddress();
            int port = datagramPacket.getPort();

            System.out.println("发送过来消息是: " + new String(data, 0, len));
            System.out.println("发送主机: " + address + "发送端口: " + port);
        }

//        System.out.println("接受端口: "+datagramSocket.getLocalPort());
//        //释放资源
//        datagramSocket.close();

    }
}
